package com.sgcc.activiti.controller;

import com.sgcc.activiti.domain.OMonTar;
import com.sgcc.activiti.service.IOMonTarService;
import com.sgcc.common.core.domain.R;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.web.page.TableDataInfo;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 监控指标Controller
 * 
 * @author sgcc
 * @date 2021-03-22
 */
@RestController
@RequestMapping("/indexMonitor")
public class OMonTarController extends BaseController
{
    @Autowired
    private IOMonTarService oMonTarService;

    /**
     * 查询监控指标列表
     */
    //@PreAuthorize(hasPermi = "activiti:indexMonitor:list")
    @GetMapping("/list")
    public TableDataInfo list(OMonTar oMonTar)
    {
        startPage();
        List<OMonTar> list = oMonTarService.selectOMonTarList(oMonTar);
        return getDataTable(list);
    }

    /**
     * 导出监控指标列表
     */
    //@PreAuthorize(hasPermi = "activiti:indexMonitor:export")
    @Log(title = "监控指标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OMonTar oMonTar) throws IOException
    {
        List<OMonTar> list = oMonTarService.selectOMonTarList(oMonTar);
        ExcelUtil<OMonTar> util = new ExcelUtil<OMonTar>(OMonTar.class);
        util.exportExcel(response, list, "indexMonitor");
    }

    /**
     * 获取监控指标详细信息
     */
    //@PreAuthorize(hasPermi = "activiti:indexMonitor:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        OMonTar data=oMonTarService.selectOMonTarById(id);
        return AjaxResult.success(data);
    }

    @Log(title = "监控指标", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addInspect")
    public AjaxResult addInspect(@RequestBody OMonTar oMonTar)
    {
        OMonTar old=oMonTarService.selectOMonTarByCode(oMonTar.getMonTarCode());
        if(null==old){
            return AjaxResult.success("可以新增监看");
        }else{
            return AjaxResult.error("监控指标编码禁止重复~");
        }

    }
    /**
     * 新增监控指标
     */
    //@PreAuthorize(hasPermi = "activiti:indexMonitor:add")
    @Log(title = "监控指标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OMonTar oMonTar)
    {
        return toAjax(oMonTarService.insertOMonTar(oMonTar));
    }

    /**
     * 修改监控指标
     */
    //@PreAuthorize(hasPermi = "activiti:indexMonitor:edit")
    @Log(title = "监控指标", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OMonTar oMonTar)
    {
        return toAjax(oMonTarService.updateOMonTar(oMonTar));
    }

    /**
     * 删除监控指标
     */
    //@PreAuthorize(hasPermi = "activiti:indexMonitor:remove")
    @Log(title = "监控指标", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(oMonTarService.deleteOMonTarByIds(ids));
    }


    @GetMapping(value = "/findCode")
    public R<OMonTar> findCode(@RequestParam("code") String code)
    {
        logger.info(code);
        OMonTar oMonTar=oMonTarService.selectOMonTarByCode(code);
        return R.ok(oMonTar);
    }
    
    @PostMapping(value = "/editStatus")
   	public AjaxResult editStatus(@RequestParam("monTarCode") String monTarCode, @RequestParam("monStatus") String monStatus) {
    	OMonTar oMonTar=oMonTarService.selectOMonTarByCode(monTarCode);
    	oMonTar.setMonStatus(monStatus);
    	return toAjax(oMonTarService.updateOMonTar(oMonTar));
    }

    @GetMapping("getMonTarByMonType")
    public AjaxResult getMonTarByMonType(String monTypeStr, String monSysCodeStr) {
        String[] monTypes = null, monSysCodes = null;
        if (StringUtils.isNotBlank(monTypeStr)) {
            monTypes = monTypeStr.split(",");
        }
        if (StringUtils.isNotBlank(monSysCodeStr)) {
            monSysCodes = monSysCodeStr.split(",");
        }
        return AjaxResult.success(oMonTarService.getMonTarByMonType(monTypes, monSysCodes));
    }
}
